package com.zretc.question.controller;

import com.zretc.api.pojo.Answer;
import com.zretc.api.pojo.Question;
import com.zretc.common.constant.SecurityConstant;
import com.zretc.common.entity.R;
import com.zretc.common.entity.RespResult;
import com.zretc.question.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ir")
public class QuestionController {
    @Autowired
    private QuestionService questionService;
    @GetMapping("/showQuestionsOfIRByAmount")
    public RespResult showQuestionsOfIRByAmount(@RequestParam("index")Integer index,
                                                @RequestParam("pageNum")Integer pageNum,
                                                @RequestParam("iRid")Integer iRid)
    {
        List<Question> result=questionService.queryQuestionsByAmountByIRId(index, pageNum, iRid);
        if(CollectionUtils.isEmpty(result))return R.fail(500,"服务器错误");
        return R.success(result);
    }
    @PostMapping("/userQuestion")
    public RespResult userQuestion(@RequestBody Question question,HttpServletRequest req)
    {
        question.setCreatedBy(Integer.valueOf(req.getHeader(SecurityConstant.AUTHENTICATION_KEY)));
        if(!questionService.giveQuestion(question))return R.fail(500,"服务器错误");
        return R.success(null);
    }
    @PostMapping("/userDeleteQuestion")
    public RespResult userDeleteQuestion(@RequestBody Map<String,Object> map, HttpServletRequest req)
    {
        Integer id=(Integer) map.get("id");
        Integer createdBy= Integer.valueOf(req.getHeader(SecurityConstant.AUTHENTICATION_KEY));
        if(!questionService.deleteQuestionByUser(id,createdBy))return R.fail(500,"服务器错误");
        return R.success(null);
    }
    @PostMapping("/publisherDeleteQuestion")
    public RespResult publisherDeleteQuestion(@RequestBody Map<String,Object> map, HttpServletRequest req)
    {
        Integer id=(Integer) map.get("id");
        Integer iRPublisherId= Integer.valueOf(req.getHeader(SecurityConstant.AUTHENTICATION_KEY));
        if (!questionService.deleteQuestionByIRPublisher(id,iRPublisherId))return R.fail(500,"服务器错误");
        return R.success(null);
    }
}
